package com.atguigu.staservice.service.impl;

import com.atguigu.staservice.client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author zr
 * @since 2021-11-26
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Resource
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        // 添加记录之前 先删除相同日期的记录  防止有多个相同记录
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated", day);
        baseMapper.delete(queryWrapper);
        // 获取注册人数
        Integer count = ucenterClient.countRegister(day);
        // 将注册人数添加到分析表里
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(count);
        // 统计日期
        sta.setDateCalculated(day);
        sta.setCourseNum(RandomUtils.nextInt(100, 200));
        sta.setLoginNum(RandomUtils.nextInt(100, 200));
        sta.setVideoViewNum(RandomUtils.nextInt(100, 200));
        baseMapper.insert(sta);
    }

    @Override
    public Map<String, Object> getChartData(String type, String begin, String end) {
        // 根据条件进行查询
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        // 查询在开始日期到截止日期间的数据
        queryWrapper.between("date_calculated", begin, end);
        // 查询指定列中的数据
        queryWrapper.select("date_calculated", type);
        List<StatisticsDaily> staList = baseMapper.selectList(queryWrapper);
        // 前段的json数组结构 对应后端的list
        // 创建用于封装日期的List集合以及日期对应的数量的集合
        List<String> dateList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();
        // 遍历查询的所有staListList集合 进行封装
        for (int i = 0; i < staList.size(); i++) {
            // 得到每一个对象
            StatisticsDaily staDaily = staList.get(i);
            // 封装日期对象
            dateList.add(staDaily.getDateCalculated());
            // 判断传过来的查询因子
            switch (type) {
                case "register_num":
                    numList.add(staDaily.getRegisterNum());
                    break;
                case "login_num":
                    numList.add(staDaily.getLoginNum());
                    break;
                case "video_view_num":
                    numList.add(staDaily.getVideoViewNum());
                    break;
                case "course_num":
                    numList.add(staDaily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("dateList", dateList);
        map.put("numList", numList);
        return map;
    }
}
